﻿using DataServer.ApplicationLib.Controls;
using DataServer.ApplicationLib.Entities;
using DataServer.ApplicationLib.Repositories;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;

namespace DataServer.ApplicationLib.Rules
{
    public class RequiredModel : ControlModelBase
    {
        private static ConcurrentDictionary<string, IList<ConfigDataCheckEntity>> CONFIGDATACHECKCACHE
             = new ConcurrentDictionary<string, IList<ConfigDataCheckEntity>>();

        public Task<VerifyRuleResult> Verify(RuleContext context, ConfigFieldEntity targetField)
        {
            if (context.ConfigInfo != null && context.ConfigInfo.CheckWay == DataCheckWay.InDB)
            {
                return RequiredVerifyInDB(context, targetField);
            }
            else
            {
                return RequiredVerifyInMemory(context, targetField);
            }
        }

        private Task<VerifyRuleResult> RequiredVerifyInMemory(RuleContext context, ConfigFieldEntity field)
        {
            VerifyRuleResult result = new VerifyRuleResult();
            // if (null != field && field.IsRequired)
            // {
            //     var msg = $"{field.FieldName}不能为空;";
            //
            //     var data = context.TargetData?
            //     .Where(i => string.IsNullOrEmpty(Convert.ToString(ReflectionHelper.GetValue(i, field.ValueColumnMap))))
            //     .ToList();
            //
            //     if (data != null && data.Any())
            //     {
            //         foreach (var item in data)
            //         {
            //             result.IsValid = false;
            //             item.IsValid = VerifyResultColumns.InvalidVal;
            //             item.Message = item.Message + msg;
            //         }
            //     }
            //
            //     if (!result.IsValid)
            //     {
            //         result.Message = msg;
            //     }
            //
            // }

            return Task.FromResult(result);
        }

        private async Task<VerifyRuleResult> RequiredVerifyInDB(RuleContext context, ConfigFieldEntity field)
        {
            VerifyRuleResult result = new VerifyRuleResult();
            if (null != field && field.IsRequired)
            {
                var repo = context.ServiceProvider.GetRequiredService<IConfigDataCheckRepository>();

                var msg = $"{field.FieldName}不能为空;";

                if (!string.IsNullOrEmpty(field.ValueColumnMap) &&
                    context.RequestId != null)
                {
                    var updatedRows = await repo.UpdateConfigDataCheck(field.ValueColumnMap, false, msg, field.ConfigId, context.RequestId);

                    if (updatedRows)
                    {
                        result.IsValid = false;
                        result.Message = msg;
                    }
                }
            }

            return result;
        }
    }
}